package game.forum;

import game.data.ProvedorDadosWEB;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class JogoImagensActivity extends Activity {

	private Gallery gallery;

	private String jogoIndex;
	private ArrayList<Bitmap> bitmaps;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fotos);
		inicializarWidgets();

		Intent intent = getIntent();
		jogoIndex = intent.getStringExtra(JogoPerfilActivity.JOGO);

		calculateBitmaps();
	}

	private void inicializarWidgets() {
		gallery = (Gallery) findViewById(R.id.galleryFotos);
	}

	private void calculateBitmaps() {
		List<String> caminhos;
		try {
			caminhos = ProvedorDadosWEB.obterURLsDeFotos(jogoIndex);
			calcularBitmapDasImagens(caminhos);
			gallery.setAdapter(new FotosAdapter(this));
		} catch (IOException e) {
			ProvedorDadosWEB.produzirMensagemSemConexao(this);
		}
	}

	private void calcularBitmapDasImagens(List<String> bitmapPaths) {
		// List<Bitmap> bitmaps = new ArrayList<Bitmap>();
		bitmaps = new ArrayList<Bitmap>();
		for (String caminho : bitmapPaths) {
			try {
				URL aURL = new URL(caminho);
				URLConnection con = aURL.openConnection();
				con.connect();
				InputStream is = con.getInputStream();
				BufferedInputStream bis = new BufferedInputStream(is);
				BitmapFactory.Options options = new BitmapFactory.Options();
				options.inSampleSize = 4;
				Bitmap bm = BitmapFactory.decodeStream(bis, null, options);
				bis.close();
				is.close();
				bitmaps.add(bm);
			} catch (MalformedURLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
		}
	}

	public class FotosAdapter extends BaseAdapter {
		Context c;

		public FotosAdapter(Context c) {
			this.c = c;
		}

		public int getCount() {
			return bitmaps.size();
		}

		public Object getItem(int position) {
			return bitmaps.get(position);
		}

		public long getItemId(int position) {
			return position;
		}

		public View getView(int position, View arg1, ViewGroup arg2) {
			ImageView im = new ImageView(c);
			im.setImageBitmap(bitmaps.get(position));
			return im;
		}
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem menuRequisitos = menu.add("Requisitos");
		MenuItem menuVideos = menu.add("Videos");
		MenuItem menuSobre = menu.add("Sobre o jogo");
		MenuItem menuPesquisa = menu.add("Pesquisa");

		menuSobre.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(getApplicationContext(),
						JogoSobreActivity.class);
				intent.putExtra(JogoPerfilActivity.JOGO, jogoIndex);
				startActivity(intent);
				return true;
			}
		});

		menuVideos.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(getApplicationContext(),
						JogoVideosActivity.class);
				intent.putExtra(JogoPerfilActivity.JOGO, jogoIndex);
				startActivity(intent);
				return true;
			}
		});

		menuRequisitos
				.setOnMenuItemClickListener(new OnMenuItemClickListener() {

					public boolean onMenuItemClick(MenuItem item) {
						Intent intent = new Intent(getApplicationContext(),
								RequisitosSistemaActivity.class);
						intent.putExtra(JogoPerfilActivity.JOGO, jogoIndex);
						startActivity(intent);
						return true;
					}
				});

		menuPesquisa.setOnMenuItemClickListener(new OnMenuItemClickListener() {

			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(getApplicationContext(),
						GameForumActivity.class);
				intent.putExtra(JogoPerfilActivity.JOGO, jogoIndex);
				startActivity(intent);
				return true;
			}
		});
		return true;
	}

}
